package com.tb.controller;

import com.tb.dubbo.IUserService;
import com.tb.utils.CaptchaUtils;
import com.tb.utils.Msg;
import com.tb.utils.SendMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author 何龙
 * @create 2019-01-10 12:36
 */
@RestController
@RequestMapping("/user")
public class EmailController {


    /**
     * @api {GET} /user/email 邮箱验证码
     * @apiDescription 发送邮箱验证码
     *  <br/><span style="color:#012456"><b>作者:何龙</b></span>
     * @apiGroup User API
     *
     *
     * @apiParam {string} email 邮箱地址
     * &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码
     *   <br/>0:正常<br/> 1:邮箱不能为空<br/> 2:邮箱格式不正确
     *   <br/>3:邮箱已经被注册 <br/>4:邮件发送失败
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data
     *
     * @apiSuccessExample {json} 正确返回值:
     *     {
     *     "code": 0,
     *     "msg": "",
     *     "data": "邮件发送成功"
     *     }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 2,
     *     "msg": "邮箱格式不正确!",
     *     "data": null
     *     }
     *
     * @apiSampleRequest  /user/email
     */

    @Autowired
    private IUserService iUserService;
    @GetMapping("/email")
    public Msg email(
            @RequestParam(value = "email",defaultValue = "")String email,HttpSession session){

        if ("".equals( email )){
            return Msg.error(1,"邮箱不能为空");
        }

        if (!emailCheck(email)){
            return Msg.error(2,"邮箱格式不正确");
        }

        iUserService.getEmail(email);
        //验证邮箱是否被注册
        if (!iUserService.getEmail(email)){
            return Msg.error( 3,"邮箱已经被注册" );
        }

        String captcha = CaptchaUtils.captcha( 6 );
        boolean b = SendMailUtil.sendEmail( email, captcha );
        if (b){
            //发送成功,将验证码存入session
            //1.移除原有emailCode
            session.removeAttribute("emailCode");
            //2.添加到session
            Map<String, String> emailCode = new HashMap<>();
            emailCode.put("email",email);
            emailCode.put( "code",captcha);
            session.setAttribute("emailCode",emailCode);
            return Msg.success( "发送成功!" );
        }else {
            return Msg.error( 4,"发送失败" );
        }

    }

    /**
     * 正则验证邮箱格式
     * @param email 邮箱
     * @return 是否合格
     */
    private boolean emailCheck(String email){
        String str="^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return Pattern.matches( str,email );
    }
    /**
     * @api {GET} /user/verification 邮箱验证码后台验证
     * @apiDescription 邮箱验证码后台验证
     *  <br/><span style="color:#012456"><b>作者:何龙</b></span>
     * @apiGroup User API
     *
     *
     * @apiParam {string} emailCode 验证码
     * &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码
     *   <br/>0:正常<br/> 1:参数不能为空<br/> 2:验证码错误
     *   <br/>3:验证码已失效
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data
     *
     * @apiSuccessExample {json} 正确返回值:
     *     {
     *     "code": 0,
     *     "msg": "",
     *     "data": "验证成功!"
     *     }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 3,
     *     "msg": "验证码已失效!",
     *     "data": null
     *     }
     *
     * @apiSampleRequest  /user/verification
     */
    @GetMapping("/verification")
    private Msg eamil(HttpSession httpSession,@RequestParam(value = "emailCode",defaultValue = "") String emailCode){
        if ("".equals( emailCode )) {
            Msg.error(1,"参数不能为空!");
        }

        try {
            Map<String, String> emailCode1 =
                    (Map<String, String>) httpSession.getAttribute( "emailCode" );
            if (emailCode.equals(emailCode1.get("code"))){
                emailCode1.put("status","ok");
                httpSession.removeAttribute("emailCode");
                httpSession.setAttribute("emailCode",emailCode1);

                return Msg.success("验证成功!");
            }else {
                return Msg.error( 2,"验证码错误!" );
            }

        }catch (Exception e){
            return Msg.error(3,"验证码已失效!");
        }
    }
}
